package com.horoscope.astrology.zodiac.signs.free.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.CharacterTextListBean

class CharacterTraitsAdapter(
    private val context: Context,
    private val data: ArrayList<CharacterTextListBean>
) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when (viewType) {
            TYPE_TITLE -> {
                return TraitsTitleViewHolder(
                    LayoutInflater.from(context).inflate(
                        R.layout.item_character_title,
                        parent,
                        false
                    )
                )
            }
            else -> {
                return TraitsViewHolder(
                    LayoutInflater.from(context).inflate(
                        R.layout.item_character_traits,
                        parent,
                        false
                    )
                )
            }
        }

    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is TraitsTitleViewHolder -> {
                if (data.size > 0) {
                    holder.title.text = data[position].title
                }
            }
            is TraitsViewHolder -> {
                if (data.size > 0) {
                    holder.title.text = data[position].title
                    holder.content.text = data[position].content
                }
            }
        }

    }

    override fun getItemViewType(position: Int): Int {
        return if (position == 0) {
            TYPE_TITLE
        } else {
            TYPE_CONTENT
        }
    }

    companion object {
        const val TYPE_CONTENT = 0x12
        const val TYPE_TITLE = 0x13
    }

    open inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    inner class TraitsViewHolder(itemView: View) : MyViewHolder(itemView) {
        val title: TextView = itemView.findViewById(R.id.tv_title)
        val content: TextView = itemView.findViewById(R.id.tv_content)
    }

    inner class TraitsTitleViewHolder(itemView: View) : MyViewHolder(itemView) {
        val title: TextView = itemView.findViewById(R.id.btn_traits)
    }

}